//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/os/cappaction.h>
#include <buola/os/capplication.h>

#include <buola/os/cprocess.h>
#include <buola/io.h>

namespace buola { namespace os {

CAppAction::CAppAction(CApplication *pApp)
    :   mApp(pApp)
{
}

std::wstring CAppAction::GetTitle()
{
    return mApp->GetName();
}

std::string CAppAction::GetIcon()
{
    return mApp->GetIcon();
}

bool CAppAction::Run(const io::CURI &pURI)
{
    const std::string &lExec=mApp->GetExec();
    std::string lCmd;

    for(int i=0;i<lExec.length();i++)
    {
        if(lExec[i]=='%')
        {
            i++;
            if(!(i<lExec.length())) break;

            switch(lExec[i])
            {
            case '%':
                lCmd+='%';
                break;

            case 'f':
            case 'F':
                lCmd+='"';
                lCmd+=pURI.Path();
                lCmd+='"';
                break;

            case 'u':
            case 'U':
                lCmd+='"';
                lCmd+=pURI.ToString();
                lCmd+='"';
                break;

            case 'i':
                if(!mApp->GetIcon().empty())
                {
                    lCmd+="--icon ";
                    lCmd+=mApp->GetIcon();
                }
                break;
                
            case 'c':
                lCmd+=utf8(mApp->GetName());
                break;
                
            case 'k':
                lCmd+=mApp->GetURI().ToString();
                break;

            default:
                msg_warn() << "unknown or deprecated field %" << lExec[i] << " executing application\n";
            }
        }
        else
        {
            lCmd+=lExec[i];
        }
    }

    if(mApp->GetStartupNotify())
        msg_warn() << "Should use startup-notification\n";

    CProcess lProcess(lExec,CProcess::DETACH|CProcess::SHELL);
    lProcess.Launch();

    return true;
}

/*namespace os*/ } /*namespace buola*/ }
